package inline.core;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class InlineActivity extends ListActivity {
	// private static final String LOG_TAG = "InlineActivity";

	private static final int BASKET_ID = Menu.FIRST;
	private static final int QUIT_ID = Menu.FIRST + 1;
	private static final int RELOAD_MARKETS_ID = Menu.FIRST + 2;

	private MarketDbAdapter mDbHelper;
	private InlineService inlineService;
	public final int ACTIVITY_SHOW_MARKET_PRODUCTS = 0;

	private Cursor marketCursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
	}

	@Override
	public void onResume() {
		super.onResume();
		inlineService = new InlineService(this);
		inlineService.open();
		mDbHelper = inlineService.getMarketDB();
		fillData();
	}
	
	/* ------------------------- Menu --------------------------------- */

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, BASKET_ID, "Ver Carrinho");
		menu.add(1, QUIT_ID, "Sair");
		menu.add(1, RELOAD_MARKETS_ID, "Carregar Mercados");
		return true;
	}
	
	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
		stopManagingCursor(marketCursor);
		inlineService.close();
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		switch (item.getId()) {

		case BASKET_ID:
			int requestCode = InlineBasketActivity.SHOW_PRODUCTS_CODE;
			Intent intent = new Intent(this,InlineBasketActivity.class);
			startSubActivity(intent, requestCode);
			return true;
		case RELOAD_MARKETS_ID:
			inlineService.reloadMarkets();
			fillData();
			return true;
		case QUIT_ID:
			finish();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);

	}

	private void fillData() {
		marketCursor = mDbHelper.fetchAllMarkets();
		startManagingCursor(marketCursor);

		// Create an array to specify the fields we want to display in the list
		// (only TITLE)
		String[] from = new String[] { MarketDbAdapter.KEY_NAME,
				MarketDbAdapter.KEY_DESCRIPTION };

		// and an array of the fields we want to bind those fields to (in this
		// case just text1)
		int[] to = new int[] { R.id.market_name, R.id.market_description };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter markets = new SimpleCursorAdapter(this,
				R.layout.market_row, marketCursor, from, to);
		setListAdapter(markets);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Cursor c = marketCursor;
		c.moveTo(position);
		Intent i = new Intent(this, InlineProductsActivity.class);
		i.putExtra(MarketDbAdapter.KEY_ROWID, (int) id);
		i.putExtra(MarketDbAdapter.KEY_NAME, c.getString(c
				.getColumnIndex(MarketDbAdapter.KEY_NAME)));
		startSubActivity(i, ACTIVITY_SHOW_MARKET_PRODUCTS);
	}

}